﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RDPCommon
{
    public partial class frmException : Form
    {
        public frmException(Exception ex)
        {
            InitializeComponent();

            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            StringBuilder sb3 = new StringBuilder();
            GetException(ex, ref sb1, ref sb2, ref sb3);
            this.txtException.Text = sb1.ToString();
            this.txtSource.Text = sb2.ToString();
            this.txtStack.Text = sb3.ToString();
            this.Activated += new EventHandler(frmException_Activated);
        }

        void frmException_Activated(object sender, EventArgs e)
        {
            this.btnClose.Focus();
        }

        void GetException(Exception ex, ref StringBuilder sb1, ref StringBuilder sb2, ref StringBuilder sb3)
        {
            if (ex != null)
            {
                if (ex.Message!=null)
                    sb1.Append(ex.Message.Trim() + System.Environment.NewLine);
                if (ex.Source != null)
                    sb2.Append(ex.Source.Trim() + System.Environment.NewLine);
                if (ex.StackTrace != null)
                    sb3.Append(ex.StackTrace.Trim() + System.Environment.NewLine);
            }            
            if (ex.InnerException!=null)
                GetException(ex.InnerException, ref sb1, ref sb2, ref sb3);
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            String msg = "" +
                this.txtException.Text.Trim() + System.Environment.NewLine +
                this.txtSource.Text.Trim() + System.Environment.NewLine +
                this.txtStack.Text.Trim() + System.Environment.NewLine;
            Clipboard.SetText(msg);
        }

        bool bExpened = false;
        private void button1_Click(object sender, EventArgs e)
        {
            if (bExpened)
            {
                this.Height = 140;
                bExpened = false;
            }
            else
            {
                this.Height = 400;
                bExpened = true;
            }
        }
    }
}
